package com.wu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wu.dao.AnswerDao;
import com.wu.dao.CounterDao;
import com.wu.pojo.po.paperPo.Answer;
import com.wu.pojo.po.paperPo.Counter;
import com.wu.service.AnswerService;
import com.wu.service.PaperService;
import com.wu.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;


@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    private AnswerDao answerDao;

    @Autowired
    private CounterDao counterDao;

    private PaperService paperService;

    // 延迟加载，避免循环注入
    @Autowired
    public AnswerServiceImpl(@Lazy PaperService paperService) {
        this.paperService = paperService;
    }


    @Override
    public List<Answer> queryAnswerByRemark(String questionId, String remark) {
        return answerDao.queryAnswerByRemark(questionId,remark);
    }


    @Override
    public List<Answer> queryAnswer() {
        return answerDao.queryAnswer();
    }


    @Override
    public List<Answer> queryAnswerByPaperId(String paperId) {
        return answerDao.queryAnswerByPaperId(paperId);
    }


    @Override
    public List<Answer> queryAnswerByQuestionId(String questionId) {
        return answerDao.queryAnswerByQuestionId(questionId);
    }


    @Override
    public Answer queryAnswerById(String id) {
        return answerDao.queryAnswerById(id);
    }

    @Transactional
    @Override
    public boolean insertAnswer(Answer answer) {
        if (answer != null && !"".equals(answer.getAnswerId())) {
            try {
                int i = answerDao.insertAnswer(answer);
                if (i == 1) {
                    return true;
                } else {
                    throw new RuntimeException("a:插入答案失败！" + answer);
                }
            } catch (Exception e) {
                throw new RuntimeException("b:插入答案失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("c:插入答案失败，Answer的id不能为空！");
        }
    }

    @Transactional
    @Override
    public boolean updateAnswer(Answer answer) {
        if (answer != null && !"".equals(answer.getAnswerId())) {
            try {
                int i = answerDao.updateAnswer(answer);
                if (i == 1) {
                    return true;
                } else {
                    throw new RuntimeException("a:更新答案失败！" + answer);
                }
            } catch (Exception e) {
                throw new RuntimeException("b:更新答案失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("c:更新答案失败，Answer的id不能为空！");
        }
    }

    @Transactional
    @Override
    public boolean deleteAnswer(String id) {
        if (id != null && !"".equals(id)) {
            try {
                int i = answerDao.deleteAnswer(id);
                if (i == 1) {
                    return true;
                } else {
                    throw new RuntimeException("a:删除答案失败！" + id);
                }
            } catch (Exception e) {
                throw new RuntimeException("b:删除答案失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("c:删除答案失败，Answer的id不能为空！");
        }
    }

    @Transactional
    @Override
    public int countAnswer(String paperId, String questionId) {
        if (paperId != null && !"".equals(paperId) && questionId != null && !"".equals(questionId)) {
            return answerDao.countAnswer(paperId, questionId);
        }
        return 0;
    }


    @Override
    public List<Answer> queryAnswerByPaperIdAndUserId(String paperId, String userId) {
        return answerDao.queryAnswerByPaperIdAndUserId(paperId,userId);
    }


    @Override
    public Answer queryAnswerByPaperIdAndUserIdAndQuestionId(String paperId, String userId, String questionId) {
        return answerDao.queryAnswerByPaperIdAndUserIdAndQuestionId(paperId,userId,questionId);
    }

    @Transactional
    @Override
    public boolean deleteAnswerByPaperId(String paperId){
        answerDao.deleteAnswerByPaperId(paperId);
        return true;
    }

    @Transactional
    @Override
    public boolean insertAnswerAndCounter(@Lazy List<Answer> answer, Counter counter) {
        // 1、答案批量入库
        for (Answer ans : answer) {
            if (null == ans.getAnswerId()) {
                ans.setAnswerId(CommonUtils.getUUID());
            }
            if (answerDao.insertAnswer(ans) != 1) {
                throw new RuntimeException("插入答案失败");
            }
        }
        // 2、得到计算问卷指标
        int indicator = paperService.getDataProcess(counter.getPaperId(), counter.getUserId());
        counter.setIndicators(indicator);
        counterDao.insertCounter(counter);//指标入库
        return true;
    }

}
